
package com.wallj.videolibrary.player;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import com.wallj.videolibrary.player.renderer.BaseRenderer;
import com.wallj.videolibrary.player.renderer.OesRenderer;
import com.wallj.videolibrary.player.renderer.WaterMarkRenderer;

public class GLVideoSurfaceView extends BaseGLSurfaceView implements BaseRenderer.RenderListener {

    //MediaPlayer.OnVideoSizeChangedListener onVideoSizeChangedListener;

    public GLVideoSurfaceView(Context context) {
        super(context);
    }

    public GLVideoSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) {
    //    this.onVideoSizeChangedListener = listener;
    //}

    public void showWaterMark(WaterMarkEntity waterMarkEntity) {
        getRenderer().showWaterMark(waterMarkEntity);
    }

    @Override
    public void onResume() {
        super.onResume();
        getRenderer().resume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //getRenderer().pause();
        getRenderer().release();
    }

    //public boolean isPlaying() {
    //    return getRenderer().isPlaying();
    //}

    //public void startPlay(String url) {
    //    getRenderer().startPlay(url);
    //}
    //
    //public void stopPlay() {
    //    getRenderer().stopPlay();
    //}

    //public MediaPlayer getMediaPlayer() {
    //    return getRenderer().getMediaPlayer();
    //}
    //
    //public boolean hasPlayerInited() {
    //    return getRenderer().hasPlayerDisplayed();
    //}
    //
    //public boolean hasPlayerPrepared() {
    //    return getRenderer().hasPlayerPrepared();
    //}

    public void onInputSizeChanged(final int width, final int height) {
        getRenderer().onInputSizeChanged(width, height);
        post(new Runnable() {
            @Override
            public void run() {
                ViewParent parent = getParent();
                if (parent != null) {
                    ViewGroup parentView = (ViewGroup) parent;
                    int parentWidth = parentView.getMeasuredWidth();
                    int parentHeight = parentView.getHeight();
                    int vWidth = parentWidth;
                    int vHeight = parentHeight;
                    if (1.0f * width / height > 1.0f * parentWidth / parentHeight) {
                        vWidth = parentWidth;
                        vHeight = (int) (1.0f * parentWidth * height / width);
                    } else {
                        vHeight = parentHeight;
                        vWidth = (int) (1.0f * width * parentHeight / height);
                    }
                    ViewGroup.LayoutParams pa = getLayoutParams();
                    if (pa == null) {
                        pa = new FrameLayout.LayoutParams(vWidth, vHeight);
                        ((FrameLayout.LayoutParams) pa).gravity = Gravity.CENTER;
                    } else {
                        pa.width = vWidth;
                        pa.height = vHeight;
                    }
                    setLayoutParams(pa);
                }
            }
        });
    }

    @Override
    protected WaterMarkRenderer getRenderer() {
        if (mRenderer == null) {
            mRenderer = new WaterMarkRenderer(getContext());
            mRenderer.setRenderListener(this);
        }
        return (WaterMarkRenderer) mRenderer;
    }

    @Override
    public void onRequestRender() {
        requestRender();
    }

    public void onDetroy() {
        getRenderer().release();
    }

    public void setTextureListener(OesRenderer.TextureListener listener) {
        getRenderer().setTextureListener(listener);
    }
}
